package com.xx.pmis.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.xx.pmis.model.Book;

public class BookDAOImpl extends BaseHibernateDAOImpl<Book> implements BookDAO {

	public Book getBookByBookID(String bookID) {
		Book book;
		try {
			book = (Book) getSession().get(Book.class, bookID);

		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return book;
	}

	@SuppressWarnings("unchecked")
	public List<Book> getAllBookByCondition(String hql) {
		List<Book> books;
		try {
			Query query = getSession().createQuery("from Book" + hql);
			books = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return books;
	}

	@SuppressWarnings("unchecked")
	public List<Book> getAllBookByConditionByPage(String hql, int currentPage, int lineSize) {
		List<Book> books;
		try {
			Query query = getSession().createQuery("from Book" + hql);
			query.setFirstResult((currentPage - 1) * lineSize);
			query.setMaxResults(lineSize);
			books = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return books;
	}
}
